package com.platform.controller;

import com.platform.entity.AuditEntity;
import com.platform.service.AuditService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program:
 * @Description: 会员管理:审核管理 包含未审核 及 审核通过
 * @Author: liweihai
 * @Date: Created in 2019/1/21 18:38
 */
@RestController
@RequestMapping("/audit")
public class AuditController {
    @Autowired
    private AuditService auditService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("audit:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AuditEntity> adList = auditService.selectList(query);

        int total = auditService.count(query);

        PageUtils pageUtil = new PageUtils(adList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/save")
    @RequiresPermissions("audit:save")
    public R save(@RequestBody AuditEntity auditEntity) {
        int i = auditService.save(auditEntity);
        if (i > 0) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 查看信息
     */
    @RequestMapping("/update")
    @RequiresPermissions("audit:update")
    public R update(@RequestBody AuditEntity auditEntity) {
        AuditEntity entity = auditService.selectById(auditEntity.getId());
        if(entity.getStatu()==3){
            return R.error("无效的修改");
        }
        int i = auditService.update(auditEntity);
        if (i > 0) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("audit:info")
    public R info(@PathVariable("id") Integer id) {
        AuditEntity ad = auditService.selectById(id);

        return R.ok().put("audit", ad);
    }

    /**
     * 已通过
     */
    @RequestMapping("/list-pass")
    @RequiresPermissions("audit:list")
    public R listPass(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AuditEntity> adList = auditService.selectListPass(query);
        int total = auditService.countPass(query);

        PageUtils pageUtil = new PageUtils(adList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 保存
     */
    @RequestMapping("/pass/{id}")
    @RequiresPermissions("audit:pass")
    public R pass(@PathVariable("id") Integer id) {
        AuditEntity entity = auditService.selectById(id);

        if(entity.getStatu()==3){
            return R.error("无效的修改");
        }
        boolean flag = auditService.checkAudit(entity.getUserId());
        if(flag){
            return R.error("用户已认证");
        }
        boolean result = auditService.updateById(id, 2);
        if(!result){
            return R.error("修改失败");
        }
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/reject/{id}")
    @RequiresPermissions("audit:pass")
    public R reject(@PathVariable("id") Integer id) {

        boolean result =auditService.updateById(id, 3);
        if(result){
            return R.ok();
        }
        return R.error("失败请检查用户是否已认证");

    }

    /**
     * 查看
     */
    @RequestMapping("/info-pass/{id}")
    @RequiresPermissions("audit:info")
    public R update(@PathVariable Long id) {
        AuditEntity entity = auditService.selectPassById(id);

        return R.ok().put("audit", entity);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("audit:delete")
    public R delete(@RequestBody Integer[] ids) {
        auditService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/pass/delete")
    @RequiresPermissions("audit:delete")
    public R passDelete(@RequestBody Integer[] ids) {
        auditService.deleteBatchPass(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AuditEntity> list = auditService.queryList(params);

        return R.ok().put("list", list);
    }
}
